import express from 'express';
import db from "./../lib/DbHelper";

let router = express.Router();

//登录接口
router.post("/login",(req,res,next) => {
	let sql = "SELECT uname FROM userinfo WHERE uname = ? AND upsw = ?";
	let params = [req.body.uname,req.body.upsw];
	
	//返回结果
	var resultObj = {"msg":"登录失败",status:-1};
	
	db.query(sql,params).then((result) => {
		if(result.length > 0){
			// console.log(result[0]);
			resultObj.data = result[0];
			resultObj.msg = "登录成功";
			resultObj.status = 1;
		}
		res.json(resultObj)
	}).catch((err) => {
		resultObj.err = err;
		res.json(resultObj);
	})
})

//注册接口
router.post("/reg",(req,res,next) => {
	let sql = "INSERT INTO userinfo (uname,upsw)VALUES(?,?);";
	let params = [
		req.body.uname,
		req.body.upsw
	];
	
	db.queryAsync(sql,params).then((data) => {
		if(data.affectedRows == 1){
			res.json({
				"msg":"注册成功",
				status:1
			})
		}else{
			res.json({
				"msg":"注册失败",
				status:-1,
			})
		}
	},(err) => {
		res.json({
			"msg":"注册失败",
			status:-2,
			err
		})
	})
	
	
	
})

module.exports = {
	router
}